package com.neusoft.yuejian_01.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.AttentionBean;
import com.neusoft.yuejian_01.javabean.ClubBean;
import com.neusoft.yuejian_01.javabean.IsAttentionBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.AttentionModel;
import com.neusoft.yuejian_01.model.ClubModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;

import it.sephiroth.android.library.picasso.Picasso;

public class ClubDetailActivity extends BaseActivity implements RetrofitListener{
    private TextView clubaddressView;
    private TextView clubtimeView;
    private TextView clubphoneView;
    private TextView clubintroduceView;
    private TextView clubinfoView;
    private TextView clubtitleView;
    protected Context context;
    private Button courseButton,videoButton;//课程列表按钮、视频列表按钮
    private ImageButton button_back,clubButton;//返回按钮、关注俱乐部按钮
    public ImageView clubImageView;
    private int c_id;
    private String c_name;
    private boolean flags;

    protected void initView() {
        setContentView(R.layout.activity_club_detail);
        clubtitleView = (TextView) findViewById(R.id.title_club);
        clubaddressView = (TextView) findViewById(R.id.club_address);
        clubtimeView = (TextView) findViewById(R.id.club_time);
        clubphoneView = (TextView) findViewById(R.id.club_phone);
        clubintroduceView = (TextView) findViewById(R.id.club_introduce);
        clubinfoView = (TextView) findViewById(R.id.club_info);
        clubImageView = (ImageView) findViewById(R.id.clubdetal_image);
        videoButton= (Button) findViewById(R.id.video_button);
        clubButton = (ImageButton) findViewById(R.id.clubButton);
        courseButton = (Button) findViewById(R.id.course_button);
        button_back = (ImageButton) findViewById(R.id.button_back);
    }
    protected void initEvents() {
        clubButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectClub(String.valueOf(getUser_id()), String.valueOf(c_id),ClubDetailActivity.this);
            }
        });
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        courseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClubDetailActivity.this, CourseActivity.class);
                intent.putExtra("c_name", c_name);
                intent.putExtra("c_id", c_id);
                startActivityForResult(intent, 1);
            }
        });
        videoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClubDetailActivity.this, VideoActivity.class);
                intent.putExtra("c_name", c_name);
                intent.putExtra("c_id", c_id);
                startActivityForResult(intent, 1);
            }
        });
    }
    protected void initData() {
        c_id = getIntent().getIntExtra("c_id", -1);
        c_name = getIntent().getStringExtra("c_name");
        ClubModel clubModel = new ClubModel();
        clubModel.clubDetail(String.valueOf(c_id), this);
        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollected(String.valueOf(getUser_id()), String.valueOf(c_id), "4", this);//flag=4.判断俱乐部关注状态
    }
    @Override
    public void onSuccess(Object object,int flag) {
        switch (flag){
            case Const.ClubDetail:
                //flag标识为1，获取俱乐部详情
                ClubBean clubDetail = (ClubBean) object;
                clubtitleView.setText(clubDetail.getC_name());
                clubaddressView.setText(clubDetail.getC_address());
                clubtimeView.setText(clubDetail.getC_time());
                clubphoneView.setText(clubDetail.getC_phone());
                clubintroduceView.setText("\u3000\u3000" + clubDetail.getC_introduce());
                clubinfoView.setText("\u3000\u3000" + clubDetail.getC_info());
                final String clubPic = ConstPhoto.BASE_URL + ConstPhoto.ClubFile + clubDetail.getC_pic();
                Picasso.with(context)
                        .load(clubPic)
                        .placeholder(R.drawable.pic2)
                        .into(clubImageView);
                break;
            case Const.IsAttention:
                //flag标识为2，判断关注状态
                IsAttentionBean isAttentionBean = (IsAttentionBean) object;
                if (isAttentionBean.getCollected().equals("1")) {
                    //已关注
                    flags=true;
                    clubButton.setImageResource(R.drawable.ic_attention_checked);
                }
                else {
                    //未关注
                    flags=false;
                    clubButton.setImageResource(R.drawable.ic_attention);
                }
                break;
            case Const.ClubAttention:
                AttentionBean attentionBean = (AttentionBean) object;
                if(attentionBean.getSuccess().equals("1")) {
                    Log.e(attentionBean.getSuccess(),"---------");
                    if(flags){
                        flags=false;
                        clubButton.setImageResource(R.drawable.ic_attention);
                    }else {
                        flags=true;
                        clubButton.setImageResource(R.drawable.ic_attention_checked);
                    }
                   Toast.makeText(ClubDetailActivity.this, "OK", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();
    }
}
